package com.qingmi.farmer.ui.order.grain;

import android.databinding.ObservableField;

import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.model.GrainDemand;
import com.qingmi.farmer.data.model.GrainOrder;
import com.wjjun.base.ui.base.BaseActivityViewModel;

/**
 * 粮食订单详情的ViewModel
 */
public class GrainOrderDetailViewModel extends BaseActivityViewModel {

    //状态
    public final ObservableField<Integer> status = new ObservableField<>(0);

    public final ObservableField<Long> updateTime = new ObservableField<>();

    //收购商名字
    public final ObservableField<String> buyerName = new ObservableField<>();

    //图标
    public final ObservableField<String> icon = new ObservableField<>();

    //品种
    public final ObservableField<String> variety = new ObservableField<>();

    //等级
    public final ObservableField<String> level = new ObservableField<>();

    //单价
    public final ObservableField<Double> price = new ObservableField<>(0.0);

    //单价
    public final ObservableField<Double> oldPrice = new ObservableField<>(0.0);

    //总量
    public final ObservableField<Integer> count = new ObservableField<>(0);
    //总量
    public final ObservableField<Integer> oldCount = new ObservableField<>(0);

    //需求量
    public final ObservableField<Integer> demanded = new ObservableField<>(0);

    //单位
    public final ObservableField<String> unit = new ObservableField<>();

    public final ObservableField<String> timeFormat = new ObservableField<>("yyyy-MM-dd");

    public final ObservableField<Long> startTime = new ObservableField<>(0L);

    public final ObservableField<Long> endTime = new ObservableField<>(0L);

    //总价
    public final ObservableField<Double> totalPrice = new ObservableField<>(0.0);

    //订单号
    public final ObservableField<String> orderId = new ObservableField<>();

    //下单时间
    public final ObservableField<Long> orderTime = new ObservableField<>();

    //上报时间
    public final ObservableField<Long> reportTime = new ObservableField<>(0L);

    //完成时间
    public final ObservableField<Long> completeTime = new ObservableField<>(0L);

    //供应方式
    public final ObservableField<Integer> delivery = new ObservableField<>();

    //库存地址
    public final ObservableField<String> address = new ObservableField<>();

    public void bindData(GrainOrder grainOrder) {

        status.set(grainOrder.getStatus());
        updateTime.set(grainOrder.getUpdateStatusAt());

        GrainDemand grainDemand = grainOrder.getGrainDemand();
        if (grainDemand != null) {
            if (grainDemand.getBuyer() != null) {
                buyerName.set(grainDemand.getBuyer().getBuyerName());
            }

            demanded.set(grainDemand.getDemandNum());
            startTime.set(grainDemand.getBeginAt());
            endTime.set(grainDemand.getEndAt());

            if (grainDemand.getGrain() != null) {
                icon.set(grainDemand.getGrain().getIcon());
                variety.set(grainDemand.getGrain().getGrainTypeName());
                level.set(grainDemand.getGrain().getLevelName());
            }
        }


        if (grainOrder.getAddress() != null) {
            address.set(grainOrder.getAddress().getAddress());
        }

        delivery.set(grainOrder.getSupplyTypeId());

        orderId.set(grainOrder.getOrderNo());
        orderTime.set(grainOrder.getCreateAt());
        reportTime.set(grainOrder.getReportTime());
        completeTime.set(grainOrder.getCompleteTime());

        if (grainOrder.getStatus() == DataSetting.GRAIN_ORDER_STATUS_PENDING_REPORTED) {//未上报实际价格前
            price.set(grainOrder.getEstimatedPrice());
            count.set(grainOrder.getEstimatedNum());
            totalPrice.set(grainOrder.getEstimatedTotalMoney());

        } else {    //已经上报过
            price.set(grainOrder.getActualPrice());
            oldPrice.set(grainOrder.getEstimatedPrice());
            count.set(grainOrder.getActualNum());
            oldCount.set(grainOrder.getEstimatedNum());
            totalPrice.set(grainOrder.getActualTotalMoney());
        }
    }

}
